# filename: emailPy.py
# -*- coding: utf-8 -*-
# !/usr/bin/env python

import smtplib
import os
import sys
import smtplib
import json
from email.mime.text import MIMEText

# mail configure
mail_to_list = ["xxxxxx@qq,com", "xxxxxx@qq.com"]
mail_host = "smtp.qq.com"
mail_user = "xxxxxx@qq.com"
mail_pass = "xxxxxx"
mail_postfix = "qq.com"
mail_host_port = 465


def send_mail_core(mail_to_list, mail_title, mail_content):
    me = "xxxxxxxxx@qq.com"
    msg = MIMEText(mail_content, _subtype='html', _charset='utf-8')

    msg["Subject"] = mail_title
    msg["From"] = me
    msg["To"] = ";".join(mail_to_list)
    try:
        s = smtplib.SMTP_SSL()
        s.connect(mail_host + ':' + str(mail_host_port))
        s.login(mail_user, mail_pass)
        s.sendmail(me, mail_to_list, msg.as_string())
        s.close()
        return True
    except Exception as e:
        print(str(e))
        return False

if __name__ == "__main__":
    try:
        jsonresult = json.loads(sys.argv[1]);
        if jsonresult["rtcode"] == 0:
            mail_title = jsonresult["data"]["title"]
            mail_content = jsonresult["data"]["url"]
            if send_mail_core(mail_to_list, mail_title, mail_content):
                print("[*] Send mail success: %s") % (mail_title)
                sys.exit()
            else:
                print("[*] Send mail failed: %s") % (mail_title)
        else:
            print("[*] Upload failed" + "  Error=  " + jsonresult["data"]["msg"])
    except Exception as e:
        print(str(e))
        raise
